package com.kunlun.common.config;

import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ObjectUtils;

@Configuration
@ConditionalOnProperty(name = "kunlun.openApi.closed", havingValue = "false", matchIfMissing = true)
public class SpringDocAutoConfig {

    private Logger log = LogManager.getLogger();

    @Value("${spring.application.name}")
    private String name;

    @Bean
    public OpenAPI customApi() {
        log.info("SpringDocAutoConfig customApi name => " + name);
        OpenAPI openAPI = new OpenAPI();
        Info info = new Info();
        info.title("SpringDoc API");
        info.description(ObjectUtils.isEmpty(name) ? "default" : name + " OpenAPI");
        info.version("V1.3.0");

        ExternalDocumentation externalDocumentation = new ExternalDocumentation();
        externalDocumentation.description("SpringDoc Wiki Documentation").url("https://springdoc.org");
        openAPI.info(info).externalDocs(externalDocumentation);
        log.info("SpringDocAutoConfig customApi openAPI => " + JSONObject.toJSONString(openAPI));
        return openAPI;
    }
}
